package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao {
    //定义一个老师类型的数组，长度为5，未来这个数组容器可以容纳5个学生对象
    private  static  Teacher[] teas =new Teacher[5];

    //添加老师
    public boolean addTeacher(Teacher tea) {
        int index =-1;
        for (int i = 0; i <teas.length ; i++) {
           if(teas[i]==null){
               index =i;
               break;
           }
        }
        if(index ==-1){
            return false;
        }else {
            teas[index] =tea;
            return true;
        }
    }

    //查询所有老师
    public Teacher[] findAllTeacher() {
        return teas;
    }

    //删除老师
    public void deleteTeacher(String delId) {
        int index =getIndex(delId);
        teas[index]=null;
    }

   //修改老师
    public void updateTeacher(String updateId, Teacher newTea) {
       int index =getIndex(updateId);
       teas[index] =newTea;
    }

    //根据教师号查找教师对象在数组中的索引位置
    private  int getIndex(String id){
        int index =-1;
        for (int i = 0; i < teas.length; i++) {
            Teacher tea =teas[i];
            if(tea !=null&&id !=null&&id.equals(tea.getId()))
                index =i;
        }
        return index;
    }
}
